/**
 * Copyright(c) 2006-2008, FeyaSoft Inc.
 */
package("uspiralweb.user");

/**
 * This JS is mainly used to handle action in
 * edit demo user.
 * Load data from server and assign fields in the form.
 *
 * @author fzhuang
 * @Date Oct 7, 2007
 */
//uspiralweb.user.edit = function(selectedId) {
uspiralweb.user.edit = function(selectedId) {

  //var User   = Ext.data.Record.create([{name: 'number'},{name: 'street'}])
  var User = Ext.data.Record.create([
    {name: "id", mapping:"id", type: "string"},
    {name: "uname", mapping:"uname", type: "string"},
    {name: "password", mapping:"password", type: "string"},
    {name: "email", mapping:"email", type: "string"},
    {name: "credits", mapping:"credits", type: "string"},
    {name: "lookup", mapping:"lookup", type: "string"},
    {name: "petsallowed", mapping:"petsallowed", type: "int"},
    {name: "lastseen", mapping:"lastseen", type: "string"},
    {name: "firstname", mapping:"firstname", type: "string"},
    {name: "lastname", mapping:"lastname", type: "string"},
    {name: "dateofbirth", mapping:"dateofbirth", type: "date"}]);

  var reader = new Ext.ux.ObjectReader({}, User);

  var data_store = new Ext.ux.WriteStore({
    proxy: new Ext.data.DWRProxy(Admin.getUser, false),
    updateProxy: new Ext.ux.DwrWriteProxy(Admin.setUser),
    replaceWrite: true,
    reader: reader});

  // pre-define fields in the form
	var lastname  = newTextField(  'Last Name','90%','lastname');
  var firstname = newTextField(  'First Name','90%','firstname');
  var username  = newTextField(  'User Name','90%','uname');
  var password  = newPasswdField('Password','90%','password');
  var email     = newTextField(  'Email','90%','email');
  var date      = newDateField(  'Date of Birth','60%','dateofbirth');
  //var note      = newTextArea(   'note','100% -35');

    // create form panel
    var formPanel = new Ext.form.FormPanel({

        baseCls: 'x-plain',
        labelWidth: 75,
        //url:'demouser/listDemouser.htm?action=editDemouser',
        reader: reader,
        //reader: new Ext.data.JsonReader({
        //    root: 'results'
        //}, ['id','username','lastname','firstname','password','email','birthday','note']
        //),
        items: [
            lastname,
            firstname,
            username,
            password,
            email,
            date
		]
	});

  data_store.on('load', function()
  {
	  // data loaded, do something with it here...
    lastname.setValue( data_store.getAt(0).data.lastname);
    firstname.setValue(data_store.getAt(0).data.firstname);
    username.setValue( data_store.getAt(0).data.uname);
    password.setValue( data_store.getAt(0).data.password);
    email.setValue(    data_store.getAt(0).data.email);
    date.setValue(     data_store.getAt(0).data.dateofbirth);
//employee_active.setValue(Ext.util.Format.boolean(employee_data.getAt(0).data.active));
  });

  function submitForm()
  {
    if (formPanel.form.isValid())
    {
      data_store.getAt(0).data.lastname    =  lastname.getValue();
      data_store.getAt(0).data.firstname   = firstname.getValue();
      data_store.getAt(0).data.uname       =  username.getValue();
      data_store.getAt(0).data.password    =  password.getValue();
      data_store.getAt(0).data.email       =     email.getValue();
      data_store.getAt(0).data.dateofbirth =      date.getValue();

      data_store.commitChanges();
      window.hide();
    } 
    else
    {
      Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
    }
  }

  // load form and assign value to fields
    //formPanel.form.load({url:'demouser/listDemouser.htm?action=loadData&id='+selectedId, waitMsg:'Loading'});
    //formPanel.getForm().load();

    // define window and show it in desktop
    var window = new Ext.Window({
        title: 'Edit Existing Demo User',
        width: 500,
        height:300,
        minWidth: 300,
        minHeight: 250,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: formPanel,

        buttons: [{text: 'Save',  handler: submitForm},
                  {text: 'Cancel',handler: function(){window.hide();}
        }]
    });

    data_store.load({params:{start:0, limit:22}, arg:[selectedId]});
    //current_store = data_store;
  
    window.show();
};

uspiralweb.user.remove = function(dataStore, gridPanel) {

    // get the selected items
    var m = gridPanel.getSelections();
    if(m.length > 0)
    {
    	Ext.MessageBox.confirm('Message',
    	    'Do you really want to delete them?',
    	    function(btn) {
	    	     if(btn == 'yes')
		         {
			        for(var i = 0, len = m.length; i < len; i++)
              {
						    dataStore.remove(m[i]);
			        }
              dataStore.commitChanges();
              dataStore.load({params:{start:0, limit:10}});
			    }
		    }
		);
    }
    else
    {
    	Ext.MessageBox.alert('Error',
    	    'To process delete action, please select at least one item to continue'
    	);
    }

};